<template>
  <el-dialog
    :title="!dataForm.productId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="品牌表的ID" prop="brandId">
      <el-input v-model="dataForm.brandId" placeholder="品牌表的ID"></el-input>
    </el-form-item>
    <el-form-item label="一级分类ID" prop="oneCategoryId">
      <el-input v-model="dataForm.oneCategoryId" placeholder="一级分类ID"></el-input>
    </el-form-item>
    <el-form-item label="二级分类ID" prop="twoCategoryId">
      <el-input v-model="dataForm.twoCategoryId" placeholder="二级分类ID"></el-input>
    </el-form-item>
    <el-form-item label="三级分类ID" prop="threeCategoryId">
      <el-input v-model="dataForm.threeCategoryId" placeholder="三级分类ID"></el-input>
    </el-form-item>
    <el-form-item label="商品的供应商ID" prop="supplierId">
      <el-input v-model="dataForm.supplierId" placeholder="商品的供应商ID"></el-input>
    </el-form-item>
    <el-form-item label="商品销售价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="商品销售价格"></el-input>
    </el-form-item>
    <el-form-item label="上下架状态：0下架1上架" prop="publishStatus">
      <el-input v-model="dataForm.publishStatus" placeholder="上下架状态：0下架1上架"></el-input>
    </el-form-item>
    <el-form-item label="审核状态：0未审核，1已审核" prop="auditStatus">
      <el-input v-model="dataForm.auditStatus" placeholder="审核状态：0未审核，1已审核"></el-input>
    </el-form-item>
    <el-form-item label="生产日期" prop="productionDate">
      <el-input v-model="dataForm.productionDate" placeholder="生产日期"></el-input>
    </el-form-item>
    <el-form-item label="商品描述" prop="descript">
      <el-input type="textarea"
                :autosize="{ minRows: 3, maxRows: 5}"
                v-model="dataForm.descript"
                placeholder="商品描述"></el-input>
    </el-form-item>
    <el-form-item label="商品录入时间" prop="indate">
      <el-input v-model="dataForm.indate" placeholder="商品录入时间"></el-input>
    </el-form-item>
    <el-form-item label="最后修改时间" prop="modifiedTime">
      <el-input v-model="dataForm.modifiedTime" placeholder="最后修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          productId: 0,
          productName: '',
          brandId: '',
          oneCategoryId: '',
          twoCategoryId: '',
          threeCategoryId: '',
          supplierId: '',
          price: '',
          publishStatus: '',
          auditStatus: '',
          productionDate: '',
          descript: '',
          indate: '',
          modifiedTime: '',
          saleCount: '',
          deleted: ''
        },
        dataRule: {
          productName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          brandId: [
            { required: true, message: '品牌表的ID不能为空', trigger: 'blur' }
          ],
          oneCategoryId: [
            { required: true, message: '一级分类ID不能为空', trigger: 'blur' }
          ],
          twoCategoryId: [
            { required: true, message: '二级分类ID不能为空', trigger: 'blur' }
          ],
          threeCategoryId: [
            { required: true, message: '三级分类ID不能为空', trigger: 'blur' }
          ],
          supplierId: [
            { required: true, message: '商品的供应商ID不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '商品销售价格不能为空', trigger: 'blur' }
          ],
          publishStatus: [
            { required: true, message: '上下架状态：0下架1上架不能为空', trigger: 'blur' }
          ],
          auditStatus: [
            { required: true, message: '审核状态：0未审核，1已审核不能为空', trigger: 'blur' }
          ],
          productionDate: [
            { required: true, message: '生产日期不能为空', trigger: 'blur' }
          ],
          descript: [
            { required: true, message: '商品描述不能为空', trigger: 'blur' }
          ],
          indate: [
            { required: true, message: '商品录入时间不能为空', trigger: 'blur' }
          ],
          modifiedTime: [
            { required: true, message: '最后修改时间不能为空', trigger: 'blur' }
          ],
          saleCount: [
            { required: true, message: '销量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.productId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.productId) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/productinfo/info/${this.dataForm.productId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productName = data.productInfo.productName
                this.dataForm.brandId = data.productInfo.brandId
                this.dataForm.oneCategoryId = data.productInfo.oneCategoryId
                this.dataForm.twoCategoryId = data.productInfo.twoCategoryId
                this.dataForm.threeCategoryId = data.productInfo.threeCategoryId
                this.dataForm.supplierId = data.productInfo.supplierId
                this.dataForm.price = data.productInfo.price
                this.dataForm.publishStatus = data.productInfo.publishStatus
                this.dataForm.auditStatus = data.productInfo.auditStatus
                this.dataForm.productionDate = data.productInfo.productionDate
                this.dataForm.descript = data.productInfo.descript
                this.dataForm.indate = data.productInfo.indate
                this.dataForm.modifiedTime = data.productInfo.modifiedTime
                this.dataForm.saleCount = data.productInfo.saleCount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/productinfo/${!this.dataForm.productId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'productId': this.dataForm.productId || undefined,
                'productName': this.dataForm.productName,
                'brandId': this.dataForm.brandId,
                'oneCategoryId': this.dataForm.oneCategoryId,
                'twoCategoryId': this.dataForm.twoCategoryId,
                'threeCategoryId': this.dataForm.threeCategoryId,
                'supplierId': this.dataForm.supplierId,
                'price': this.dataForm.price,
                'publishStatus': this.dataForm.publishStatus,
                'auditStatus': this.dataForm.auditStatus,
                'productionDate': this.dataForm.productionDate,
                'descript': this.dataForm.descript,
                'indate': this.dataForm.indate,
                'modifiedTime': this.dataForm.modifiedTime,
                'saleCount': this.dataForm.saleCount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
